﻿<template>
    <my-dialog v-model="state.dlgVisible" :title="mtitle">
        <div class="card-header">
            <my-search-bar :tbname="tbname" v-model="search2" :excludeSearch="excludeSearch"></my-search-bar>
        </div>
        <my-grid :tbname="tbname" ref="grid" :selectModel="true" @selected="onSelectOne" :multi="state.isMulti"
            v-model:search="search2" :fields="fields"></my-grid>
        <template #footer>
            <el-button v-if="allowAdd" type="primary" @click="doAdd">添加</el-button>
            <el-button v-if="clearable" type="success" @click="onClear">清除</el-button>
            <el-button v-if="!hideOK" type="primary" @click="onOk">确定</el-button>
        </template>
    </my-dialog>
</template>

<script>
export default {
    name: "MySelectWin",
    props: {
        title: String,
        tbname: String,
        fields: String,
        search: {
            type: Object,
            default() {
                return {}
            }
        },
        clearable: Boolean,
        excludeSearch: String,
        allowAdd: Boolean,
        hideOK: Boolean,
    },
    emits: ["selected", "clear", "update:search"],
    data() {
        return {
            mtitle: "",
            state: {
                dlgVisible: false,
                isMulti: false,
            },
            search2: {}
        }
    },
    created() {
        this.search2 = this.search || {};
        if (!this.title && this.tbname) {
            this.$server.getDM(this.tbname).then((res) => {
                this.mtitle = (this.hideOK ? "" : "选择:") + res.title;
            });
        } else {
            this.mtitle = this.title;
        }
    },
    methods: {
        open(multi) {
            this.state.isMulti = multi;
            this.state.dlgVisible = true;
        },
        onOk() {
            var items = this.$refs.grid.getSelected();
            if (items.length > 0) {
                this.$refs.grid.clearSelection();
                this.state.dlgVisible = false
                this.$emit("selected", items);
            }
        },
        doAdd() {
            let item = { ...this.search, ID: 0 }
            $server.dlg.create(this.tbname, 0, item).then(res => {
                this.$refs.grid.loadData(1);
            })
        },
        onClear() {
            this.$refs.grid.clearSelection();
            this.state.dlgVisible = false;
            this.$emit("clear");
        },
        onSelectOne(item) {
            if (!this.hideOK) {
                this.$refs.grid.clearSelection();
                this.state.dlgVisible = false
                this.$emit("selected", [item]);
            } else {
                $server.dlg.edit(this.tbname, item.ID).then(res => {
                    this.$refs.grid.loadData();
                })
            }
        }
    },

    watch: {
        title() {
            this.mtitle = this.title;
        },
        search() {
            this.search2 = this.search;
        },
        // search2(val){
        //     this.$emit("update:search", val);
        // }
    }
}

</script>
